package com.guoxin.concurrentprogramming.chapter3.nolock;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author guoxin
 * @date 2019/7/17
 */
public class AtomicIntegerDemo {
    private static AtomicInteger i = new AtomicInteger();

    private static class MyTask extends Thread {
        @Override
        public void run() {
            for (int k = 0; k < 10000; k++) {
                i.incrementAndGet();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyTask task = new MyTask();
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(task);
            threads[i] = t;
        }

        for (Thread t : threads) {
            t.start();
        }

        for (Thread t : threads) {
            t.join();
        }
        System.out.println(i);
    }
}
